package com.cloudptt.api.product.res;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.cloudptt.api.product.log.Log;

import android.content.Context;

public class Resource {

	public static String Log_Key = "Resource";
	private static String ResouceName_ZH = "strings_zh.xml";
	private static String ResouceName_EN = "strings_en.xml";
	private static String KeyStatus = "status_";
	private static String KeySds = "sds_";
	private static String KeyFd = "fd_";
	private static HashMap<String,String> resMap = null;

	public static void init(Context ctx,Locale locale)
	{
		InputStream in = null;
		try 
		{
			String fileName = getFileName(locale);
			in = ctx.getResources().getAssets().open(fileName);
			readXmlAndInit(in);
		} 
		catch (XmlPullParserException e) 
		{
			// TODO: handle exception
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			// TODO: handle exception
			e.printStackTrace();
		}
		finally
		{
			if(in != null)
			{
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	private static String getFileName(Locale locale)
	{
		if(Locale.CHINA == locale)
		{
			return ResouceName_ZH;
		}
		else
		{
			return ResouceName_EN;
		}
	}
	private static void readXmlAndInit(InputStream in) throws IOException,XmlPullParserException
	{
		XmlPullParserFactory factory;
		factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(true);
		XmlPullParser xpp = factory.newPullParser();
		xpp.setInput(in, "UTF-8");
		xpp.nextTag();
		xpp.require(XmlPullParser.START_TAG, null, "resources");
		
		resMap = new HashMap<String,String>();
		String key = "";
		String value = "";
		while (xpp.nextTag() == XmlPullParser.START_TAG) 
		{
			xpp.require(XmlPullParser.START_TAG, null, "string");
			key = xpp.getAttributeValue(null, "name");
			value = xpp.nextText();
		    
			resMap.put(key, value);
//			Log.d(Log_Key,  "readXmlAndInit key = " + key + " value = " + value);

		    if (xpp.getEventType() != XmlPullParser.END_TAG) 
		    {
		    	xpp.nextTag();
		    }

		    xpp.require(XmlPullParser.END_TAG, null, "string");
		}
		xpp.require(XmlPullParser.END_TAG, null, "resources");
	}
	
	public static String getString(String key)
	{
		Log.d(Log_Key,  "getString key = " + key );
		if(key == null
				|| key.length()<= 0)
		{
			return "";
		}
		if(resMap == null
				|| resMap.size()<= 0)
		{
			Log.d(Log_Key,  "getString resMap = null");
			return "";
		}
		if(resMap.get(key) == null)
		{
			Log.d(Log_Key,  "getString resMap.get(key) = null");
			return "";
		}
		else
		{
			return resMap.get(key);
		}
	}
	public static String getMcdtataStatusString(Boolean isFd,int status)
	{
		String key = KeyStatus+ KeySds + status;
		if(isFd)
		{
			key = KeyStatus+ KeyFd + status;
		}
		String value =  getString(key);
		if(value == null
			|| value.trim().equals(""))
		{
			if(isFd)
			{
				value = getString(KeyStatus+ KeyFd + 3);
			}
			else {
				value = getString(KeyStatus+ KeySds + 3);
			}
		}
		return value;
	}
}
